/*
 * 本类作用：数据操作存储（上一步：业务处理Adapter，下一步：程序界面初始化DbGpsDataCeshi）
 * 
 * 作者：袁小杰
 * 博客：http://blog.csdn.net/undoner
 * GIT：https://git.oschina.net/undoner
 * QQ：15137281
 * 
 */
package org.mina.business.db;

import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;
import org.mina.business.dao.GpsCarInfoDAO;
import org.mina.business.dao.TestGpsDataDAO;
import org.mina.business.model.Gps_carinfo;
import org.mina.business.model.TestGpsData;

public class DbGpsDataCeshi {

	static Logger log = Logger.getRootLogger();

	private static SqlSessionFactory sqlSessionFactory;
	private static Reader reader;
	public static ArrayList<TestGpsData> listsite = new ArrayList<TestGpsData>();
	static {
		try {
			String filePath = System.getProperty("user.dir") + File.separator
					+ "config" + File.separator + "\\MybatisConfig.xml";
			// 绝对路径获取文件：
			reader = new FileReader(new File(filePath));
			// 如果是src目录下，可使用以下路径：
			// reader = Resources.getResourceAsReader(filePath);
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
	}

	public static SqlSessionFactory getSession() {
		return sqlSessionFactory;
	}

	public static void insertGpsDataCeshi(TestGpsData gpsDataCeshi) {
		SqlSession session = sqlSessionFactory.openSession();
		try {
			TestGpsDataDAO gpsDataCeshiMapper = session
					.getMapper(TestGpsDataDAO.class);
			gpsDataCeshiMapper.insertGpsData(gpsDataCeshi);
			session.commit();// 这里一定要提交，不然数据进不去数据库中
		} catch (Exception e) {
			log.error(e.getMessage());
		} finally {
			session.close();
		}
	}

	// car数据-获取car信息列表：对象
	public static List<Gps_carinfo> getGpsCarInfo() {
		SqlSession session = sqlSessionFactory.openSession();
		List<Gps_carinfo> list = null;
		try {
			GpsCarInfoDAO gpsCarInfoMapper = session.getMapper(GpsCarInfoDAO.class);
			list = gpsCarInfoMapper.getGpsCarInfo();
			if (list != null) {
				for (Gps_carinfo gpscarinfo : list) {
					// System.out.println(gpscarinfo.getFenceid());
				}
			}
		} finally {
			session.close();
		}
		return list;
	}

}
